%Continuation to question II_1
clc;
%Calc Plugin bayes estimated error
m1 = mean(ds1);s1 = cov(ds1);m2 = mean(ds2);s2 = cov(ds2);T = log(w2.P/w1.P);

E_pb = plugin_bayes(ds1,ds2,T,m1,s1,m2,s2,w1.P,w2.P);
disp(['E_plugin_bayes=',num2str(E_pb)])

%Calc Plugin bayes real error

E_pb_r = plugin_bayes(new1,new2,T,m1,s1,m2,s2,w1.P,w2.P);
disp(['E_plugin_bayes_real=',num2str(E_pb_r)])

%Calc the 1_NN error
[idx,d] = knnsearch([ds1;ds2],new1,'K',1);
ctr = sum(idx<=size(ds1,1),2);
ne1 = sum(ctr ~= 1);

[idx,d] = knnsearch([ds1;ds2],new2,'K',1);
ctr = sum(idx>=size(ds1,1),2);
ne2 = sum(ctr ~= 1);

E_nn = ne1 * w1.P / size(new1,1) + ne2 * w2.P / size(new2,1);
disp(['E_1nn=',num2str(E_nn)])

%Searching for overfiting
for n = 80:-10:20
    ds1_sub = ds1(1:floor(n*w1.P),:);
    ds2_sub = ds2(1:floor(n*w2.P),:);
    m1 = mean(ds1_sub);s1 = cov(ds1_sub);m2 = mean(ds2_sub);s2 = cov(ds2_sub);
    E_pb = plugin_bayes(ds1_sub,ds2_sub,T,m1,s1,m2,s2,w1.P,w2.P);
    disp(['E_plugin_bayes(n=',num2str(n),')=',num2str(E_pb)])
    if E_pb == 0
        break
    end
    if n == 50
        E_pb_r = plugin_bayes(new1,new2,T,m1,s1,m2,s2,w1.P,w2.P);
        disp(['E_overfitting(n=50)=',num2str(E_pb_r)])
    end
end
E_pb_r = plugin_bayes(new1,new2,T,m1,s1,m2,s2,w1.P,w2.P);
disp(['E_overfitting=',num2str(E_pb_r)])
%II_2;
